home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0099_Boolean String Search.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-26  |  32KB  |  494 lines

  1. Unit BoolPos;
  2. {$Define Test}
  3. {        Once debugging is complete, remove the above line to turn off debug mode. }
  4.  
  5. {        Version 1.3.5.P.
  6.  
  7.         Requires Borland Turbo Pascal version 6.0 or later to compile.
  8.  
  9.         Author:  Bruce J. Lackore.  Created Friday, July 23, 1993.
  10.         Copyright (c) 1993 Bruce J. Lackore.  ALL RIGHTS RESERVED.
  11. }
  12.  
  13. {$IFDEF Test}
  14.         {$A+,B-,D+,F-,G-,I+,L+,O-,R+,S+,V-,X+}
  15. {$ELSE}
  16.         {$A+,B-,D-,F-,G-,I-,L-,O-,R-,S-,V-,X+}
  17. {$ENDIF}
  18.  
  19. {        This unit comprises a function capable of searching a string for multiple
  20.         occurences of substrings using Boolean operators.  In the search string,
  21.         Boolean operators And and Or are defined as follows:
  22.  
  23.                 & - And
  24.                 | - Or
  25.  
  26.         Parentheses are supported for doing multiple searches.  Search strings are
  27.         submitted as follows:
  28.  
  29.                 i.e. In the source string "The quick brown fox jumped over the lazy dog"
  30.                                         and the search is for the word blue and the words quick or fox,
  31.                                         the search string is entered as follows:
  32.  
  33.                                                 (blue&(quick|fox))
  34.  
  35.         The way the function is currently written, And (&) and Or (|) have the same
  36.         precedence level hence the above search string without parentheses would be
  37.         interpretted to be (blue&quick|fox):
  38.  
  39.                 blue And quick would be searched for first, the result Or'd with the
  40.                 results of the search for fox.
  41.  
  42.         Notice the difference in that (blue&(quick|fox)) is a False statement whilst
  43.         (blue&quick|fox) is True.
  44.  
  45.         The function will automatically scan for () pairs, adding the necessary )
  46.         at the end of the search string or ( at the beginning if required.
  47.  
  48.         The function will also search for (|, |), (& and &) symbols, these being
  49.         illegal.
  50.  
  51.         It should also be noted that although excess parens will not cause the
  52.         function to fail, they DO cause the function to loop unnecessarily through
  53.         the token search (once for each set of parens) while bringing the final
  54.         answer out of the final set of parens.
  55.  
  56. }
  57.  
  58. {        Bug fixes:
  59.  
  60.                 07/04/1994: Thought the 06/01 fix did the job.  It didn't.  This time,
  61.                                                                 I went back into the token processor and found that it was
  62.                                                                 missing a left paren when the tokenized search string was in
  63.                                                                 the form of (b@b...)@(b@b...) where b is a boolean designator
  64.                                                                 (T or F) and @ is a boolean operator (| or &).  Thanx to
  65.                                                                 Michael Jarmulowicz for pointing this out.
  66.                                                                 The fix was to go into the Process_token_str function and
  67.                                                                 ensure that a multi-pass required token string has sufficient
  68.                                                                 parens so as to not confuse the token processor.
  69.                                                                 Also defined BPos return value should the Fixup_srch_str
  70.                                                                 function fail.  The default is False (as set in the first
  71.                                                                 line of the BPos function itself) and is triggered by
  72.                                                                 Fixup_srch_str returning a null string.  Removed the "fix"
  73.                                                                 that was suggested in the 06/01 bug fix and replaced it with
  74.                                                                 code that scans the first and last letters of the Srch_str
  75.                                                                 to ensure that they are parens, if not, add a pair.
  76.  
  77.                 06/01/1994: After returning from WestPac, I received a couple of emails
  78.                                                                 telling me that if the function was called with NO
  79.                                                                 parentheses, it would fail.  The fix is simply to add a set of
  80.                                                                 parens in the Fixup_srch_str function just before the
  81.                                                                 function returns if the first character of the Srch_str is NOT
  82.                                                                 a left paren equivalent.  I have had one report of the unit
  83.                                                                 not working in protected mode.  As I don't yet know much about
  84.                                                                 protected mode programming, I am still working on that
  85.                                                                 particular bug but I WILL fix it if the error is in here.  I
  86.                                                                 also tightened up one of the assembly replacement functions,
  87.                                                                 see the docs for the change.
  88.  
  89.                 10/04/1993:        Noticed that length of Src_str in function Next_CPos was
  90.                                                                 incorrectly calculated because of positioning of INC DI.
  91.                                                                 INC DI precedes the MOV CL,[ES:DI] causing the function to
  92.                                                                 consider the first character of Src_str to represent the
  93.                                                                 length rather than the actual length byte.  Fix is to move
  94.                                                                 the INC DI to the line following the MOV CL,[ES:DI].
  95.  
  96. }
  97.  
  98. Interface
  99.  
  100. Function BPos(Srch_str, Src_str:  String;  Ignore_case:  Boolean):  Boolean;
  101.  
  102. {        This function accepts a source string and a search string as described above
  103.         and returns a Boolean value based on whether or not the parsed search
  104.         string was found.
  105. }
  106.  
  107. { ************************************************************************** }
  108.  
  109. Implementation
  110.  
  111. Const
  112.         Lt_pn:                                                                                Char = '(';
  113.         Rt_pn:                                                                                Char = ')';
  114.  
  115. Function Cnt_ch(Scan_char:  Char;  In_str:  String):  Byte;  Assembler;
  116.  
  117. {        This function will scan a string for occurences of a particular character.
  118.         The function will return the number of occurences.
  119. }
  120.  
  121.         Asm  { Function Cnt_ch }
  122.                                                         XOR                AX,AX                                        {        0 AX }
  123.                                                         MOV                BL,Scan_char  {        Put char to count in BL }
  124.                                                         LES                SI,In_str     {        Set ES:SI to point to start of string }
  125.                                                         XOR                CX,CX         {        0 CX }
  126.                                                         MOV                CL,[ES:SI]    {        Move string length to CX }
  127.                                                         ADD                SI,CX         {        Set ES:SI to point to END of string }
  128.                 @LOOK:                CMP                BL,[ES:SI]    {        Start Loop, compare current char and BL }
  129.                                                         JNE                @NEXT         {        If not equal, jump to end of loop }
  130.                                                         INC                AX            { If equal, Inc char cnt (AX) }
  131.                 @NEXT:                DEC                SI            {        Set ES:SI back one character }
  132.                                                         LOOP        @LOOK         {        Decrement CX and jump to start of loop }
  133.         End;  { Function Cnt_ch }
  134.  
  135. Function Fill_str(Dupe_ch:  Char;  How_many:  Byte):  String;  Assembler;
  136.  
  137. {        This function returns How_many of Dupe_char.
  138. }
  139.  
  140.         Asm  { Function Fill_str }
  141.                                                         LES                DI, @Result                {        Set ES:DI to function result area }
  142.                                                         CLD                 {        Clear direction flag }
  143.                                                         XOR         CH,CH         {        0 CH }
  144.                                                         MOV         CL,How_many          { Length in CX }
  145.                                                         MOV         AX,CX                { and in AX }
  146.                                                         STOSB                     { Store length byte }
  147.                                                         MOV         AL,Dupe_ch    {        Put char to dupe in AL }
  148.                                                         REP         STOSB         { Fill string with char }
  149.         End;  { Function Fill_str }
  150.  
  151. Function PosC(Srch_ch:  Char;  Src_str:  String):  Boolean;  Assembler;
  152.  
  153. {        This function is similar to the Pos function of Pascal except that it
  154.         accepts only a single character to search for.  This function returns a
  155.         True if a Srch_ch is encountered, a False if not.
  156. }
  157.  
  158.         Asm  { Function PosC }
  159.                                                         XOR                BX,BX                                        {        0 BX }
  160.                                                         MOV                AL,Srch_ch    {        Put char to look for in AL }
  161.                                                         LES                DI,Src_str    {        Set ES:DI to start of Src_str }
  162.                                                         XOR                CX,CX         {        0 CX }
  163.                                                         MOV                CL,[ES:DI]    {        Store length of Src_str in CL }
  164.                                                         ADD                DI,CX         {        Set ES:DI to end of string }
  165.                                                         STD                 {        Set direction flag }
  166.                 @LOOK:                REPNZ        SCASB         {        Look for AL in Src_str }
  167.                                                         JNZ                @DONE         {        If not found, jump to end (BX = 0) }
  168.                                                         INC                BX            {        If Found, Inc Bx  to 1 = Pascal True }
  169.                 @DONE:                MOV                AX,BX         {        Move BX to AX (return result) }
  170.         End;  { Function PosC }
  171.  
  172. Function Last_Cpos(Srch_ch:  Char;  Src_str:  String):  Byte;  Assembler;
  173.  
  174. {        This function performs the same function as the Pascal POS function except
  175.         that it works only with a single character and rather than returning the
  176.         first position the character is found in, it returns the LAST position that
  177.         the search character is found in.
  178. }
  179.  
  180.         Asm { Function Last_Cpos }
  181.                                                         MOV                AL,Srch_ch                {        Put char to look for in AL }
  182.                                                         LES                DI,Src_str    {        Set ES:DI to start of Src_str }
  183.                                                         XOR                CX,CX         {        0 CX }
  184.                                                         MOV                CL,[ES:DI]    {        Move length of Src_str to CL }
  185.                                                         ADD                DI,CX         {        Set ES:DI to end of Src_str }
  186.                                                         INC                CX            { Add one to CX (correct for string length }
  187.                                                         STD                 {        Set direction flag }
  188.                                                         REPNZ        SCASB         {        Look for character in string }
  189.                                                         MOV                AX,CX         { If found CX indicates position, else 0 }
  190.         End;  { Function Last_Cpos }
  191.  
  192. Function Next_CPos
  193.         (Srch_ch:  Char;  Src_str:  String;  Strt_at:  Byte):  Byte;  Assembler;
  194.  
  195. {        This function searches for the next occurence of Srch_ch in Src_str AFTER
  196.         position Strt_at.  The function returns the offset from the beginning of
  197.         the string, NOT the offset from Strt_at.
  198. }
  199.  
  200.         Asm  { Function Next_CPos }
  201.                                                         XOR                AX,AX         {        0 AX }
  202.                                                         MOV                AL,Strt_at    {        Move position to start at to AL }
  203.                                                         LES                DI,Src_str    {        Set ES:DI to start of Src_str }
  204.                                                         XOR                CX,CX         {        0 CX }
  205.                                                         MOV                CL,[ES:DI]    {        Store length of Src_str in CL }
  206.                                                         INC                DI            {        Set ES:DI to first char of Src_str }
  207.                                                         MOV                BX,CX         {        Move CX to BX }
  208.                                                         SUB                CX,AX         {        Set CX to length of string after Strt_at }
  209.                                                         ADD                DI,AX         {        Set ES:DI to char at Strt_at in Src_str }
  210.                                                         MOV                AL,Srch_ch    {        Move Srch_ch to AL }
  211.                                                         CLD                 {        Clear direction flag }
  212.                                                         REPNZ        SCASB         {        Look for character following Strt_at }
  213.                                                         JNZ                @NOTFND       {        If not found, jump to end of procedure }
  214.                                                         SUB                BX,CX         {        Set BX to position char found in }
  215.                                                         JMP                @DONE         {        Jump to end of procedure }
  216.                 @NOTFND:        XOR                BX,BX         {        Srch_ch not found, set BX to 0 }
  217.                 @DONE:                MOV                AX,BX         {        Move position found at (BX) to AX }
  218.         End;  { Function Next_CPos }
  219.  
  220. {$F+}
  221. Function Up_cs(In_str:  String):  String;
  222.  
  223. {        This function converts In_str to all upper case characters.
  224. }
  225.  
  226.         Begin  { Function Up_cs }
  227.                 Inline(
  228.                         $1E/                                                                {                                        PUSH DS  }
  229.                         $C4/$7E/$0A/                                {                                        LES         DI,[BP+$0A]  }
  230.                         $C5/$76/$06/                                {                                        LDS         SI,[BP+$06]  }
  231.                         $30/$E4/                                                {                                        XOR         AH,AH  }
  232.                         $AC/                                                                {                                        LODSB  }
  233.                         $AA/                                                                {                                        STOSB  }
  234.                         $89/$C1/                                                {                                        MOV         CX,AX  }
  235.                         $E3/$0F/                                                {                                        JCXZ DONE  }
  236.                         $FC/                                                                {                                        CLD  }
  237.                         $AC/                                                                {DOCHAR:        LODSB  }
  238.                         $3C/$61/                                                {                                        CMP         AL,'a'  }
  239.                         $72/$06/                                                {                                        JB         NEXTCH  }
  240.                         $3C/$7A/                                                {                                        CMP         AL,'z'  }
  241.                         $77/$02/                                                {                                        JA         NEXTCH  }
  242.                         $24/$DF/                                                {                                        AND         AL,$DF  }
  243.                         $AA/                                                                {NEXTCH:        STOSB  }
  244.                         $E2/$F2/                                                {                                        LOOP DOCHAR  }
  245.                         $1F)                                                                {DONE:                POP         DS  }
  246.         End;  { Function Up_cs }
  247. {$F-}
  248.  
  249. Function Fixup_srch_str(Srch_str:  String):  String;
  250.  
  251. {        This functions sole purpose in life is to count the number of parantheses
  252.         pairs and correct for a deficient number of either by adding the appropriate
  253.         character either at the beginning or the end of the search string.  This
  254.         may not yield the correct result as the searcher intended but is a
  255.         requirement of the algorithm (it searches for paran pairs).  Note that the
  256.         function will add one set of parantheses if none are found.  This function
  257.         also looks for illegal character pairs (&, &), (| and |), these pairs
  258.         indicate an illegal Boolean search.  The function returns the corrected
  259.         Srch_str if all is well, an empty string if not.
  260. }
  261.  
  262.         Var
  263.                 Left_para,
  264.                 Right_para,
  265.                 How_many:                                                                Integer;
  266.  
  267.         Begin  { Function Fixup_srch_str }
  268.                 If (Srch_str[Length(Srch_str)] <> Rt_pn) Or (Srch_str[1] <> Lt_pn) Then
  269.                         Srch_str := Lt_pn + Srch_str + Rt_pn;
  270.                 Left_para         := Cnt_ch(Lt_pn, Srch_str);                                        {        Count the parens }
  271.                 Right_para         := Cnt_ch(Rt_pn, Srch_str);
  272.                 How_many                 := Abs(Left_para - Right_para);     { Get the difference }
  273.                 If How_many > 0 Then
  274.                         If Right_para < Left_para Then
  275.                                 Srch_str := Srch_str + Fill_str(Rt_pn, How_many)
  276.                         Else
  277.                                 Srch_str := Fill_str(Lt_pn, How_many) + Srch_str;
  278.                 If (Pos(Lt_pn + '&', Srch_str) <> 0) Or         { Illegal call? }
  279.                         (Pos('&' + Rt_pn, Srch_str) <> 0) Or
  280.                         (Pos(Lt_pn + '|', Srch_str) <> 0) Or
  281.                         (Pos('|' + Rt_pn, Srch_str) <> 0) Then
  282.                                 Fixup_srch_str := ''
  283.                 Else
  284.                         Fixup_srch_str := Srch_str                                                                                {        All is well }
  285.         End;  { Function Fixup_srch_str }
  286.  
  287. Function Parse_srch_str(Srch_str, Src_str:  String):  String;
  288.  
  289. {        This function simply extracts each string to search for, tests to see if
  290.         it exists in the original string and replaces the extracted substring with
  291.         the appropriate token.  It should be noted that each substring is determined
  292.         solely by the characters used for parantheses and operators.  Any other
  293.         characters are assumed to be part of the search string.
  294.  
  295.         Each substring is searched for in the original Search_str and its presense
  296.         or absense noted with a T or F respectively.
  297. }
  298.  
  299.         Var
  300.                 Rtn_str,
  301.                 Token_str:                                                        String;
  302.                 End_token:                                                        Boolean;
  303.  
  304.         Begin  { Function Parse_srch_str }
  305.                 Token_str         := '';
  306.                 Rtn_str                        := '';
  307.                 While Srch_str <> '' Do
  308.                         Begin
  309.                                 If (Srch_str[1] In [Lt_pn, Rt_pn, '&', '|']) Then { Token starts? }
  310.                                         Begin
  311.                                                 End_token := (Token_str <> '');       { End of token?  If not }
  312.                                                 If Not(End_token) Then                { then start one.       }
  313.                                                         Rtn_str := Rtn_str + Srch_str[1]
  314.                                         End
  315.                                 Else
  316.                                         Begin
  317.                                                 Token_str := Token_str + Srch_str[1]; { Add a char to substring }
  318.                                                 End_token        := False
  319.                                         End;
  320.                                 If End_token Then                         { If complete token, look }
  321.                                         Begin                                   { for it in the source str }
  322.                                                 If Pos(Token_str, Src_str) <> 0 Then
  323.                                                         Rtn_str := Rtn_str + 'T'            { If found, return T }
  324.                                                 Else
  325.                                                         Rtn_str := Rtn_str + 'F';           { If not, return F   }
  326.                                                 Rtn_str         := Rtn_str + Srch_str[1];
  327.                                                 Token_str := '';                      { Reset to look for more }
  328.                                                 End_token        := False
  329.                                         End;  { If End_token }
  330.                                 Delete(Srch_str, 1, 1)                    { Delete the char just
  331.                                                                                                                                                                                                                 processed and start again
  332.                                                                                                                                                                                                         }
  333.                         End;  { While Srch_str <> '' }
  334.                 Parse_srch_str := Rtn_str
  335.         End;  { Function Parse_srch_str }
  336.  
  337. Function Process_token_str(Token_str:  String):  Char;
  338.  
  339.         Var
  340.                 One_token:                                                        String;
  341.                 One_token_len,
  342.                 Left_para:                                                        Byte;
  343.  
  344.         Function Process_one_token_str(The_token:  String):  Char;
  345.  
  346.                 Var
  347.                         Lcv:                                                                        Byte;
  348.                         Curr_answer,
  349.                         Do_and:                                                                Boolean;
  350.  
  351.                 Begin  { Function Process_one_token_str }
  352.                         Curr_answer := (The_token[1] = 'T');      { Establish current answer
  353.                                                                                                                                                                                                         by checking first token.
  354.                                                                                                                                                                                                 }
  355.                         For Lcv := 2 to Length(The_token) Do      { Look at the rest of the
  356.                                                                                                                                                                                                         token str.
  357.                                                                                                                                                                                                 }
  358.                                 Case The_token[Lcv] of                  { Boolean op is And }
  359.                                         '&':        Do_and := True;                 { Boolean op is Or }
  360.                                         '|':        Do_and := False;
  361.                                         'T':        If Do_and Then
  362.                                                                         Curr_answer := Curr_answer And True  { If And }
  363.                                                                 Else
  364.                                                                         Curr_answer := True;                 { If Or }
  365.                                         'F':        If Do_and Then                         { If And (Or stays T) }
  366.                                                                         Curr_answer := False;
  367.                                 End;  { Case }
  368.                         If Curr_answer Then                      { Final result }
  369.                                 Process_one_token_str := 'T'
  370.                         Else
  371.                                 Process_one_token_str        := 'F'
  372.                 End;  { Function Process_one_token_str }
  373.  
  374.         Begin  { Function Process_token_str }
  375.  
  376.                 { Are parens present?  If so process as tokenized phrase, if not, final
  377.                         result has been received or can be processed in a single pass.
  378.                 }
  379.  
  380.                 If PosC(Lt_pn, Token_str) Then
  381.                         Begin
  382.                                 While Length(Token_str) > 1 Do
  383.                                         Begin
  384.  
  385.                                                 {        Ensure that the token has enough parens to not confuse the
  386.                                                         token string processor.  One need only check for a left paren
  387.                                                         since the Fixup_srch_str function ensures that an equal number
  388.                                                         of paren PAIRS exists.
  389.                                                 }
  390.  
  391.                                                 If Not(PosC(Lt_pn, Token_str)) Then
  392.                                                         Token_str := Lt_pn + Token_str + Rt_pn;
  393.  
  394.                                                 { Find leftmost left paren }
  395.  
  396.                                                 Left_para                 := Last_Cpos(Lt_pn, Token_str);
  397.  
  398.  
  399.                                                 { Find first right paren after leftmost left paren }
  400.  
  401.                                                 One_token_len :=
  402.                                                         Succ(Next_CPos(Rt_pn, Token_str, Left_para) - Left_para);
  403.  
  404.                                                 { Copy everything between the two }
  405.  
  406.                                                 One_token := Copy(Token_str, Left_para, One_token_len);
  407.  
  408.                                                 { Remove the parens }
  409.  
  410.                                                 Dec(One_token[0]);
  411.                                                 Delete(One_token, 1, 1);
  412.  
  413.                                                 { Remove the original substring from the phrase }
  414.  
  415.                                                 Delete(Token_str, Left_para, One_token_len);
  416.  
  417.                                                 { Insert the resultant single character in place of the old
  418.                                                         substring.
  419.                                                 }
  420.  
  421.                                                 Insert(Process_one_token_str(One_token), Token_str, Left_para)
  422.                                         End;  { While Length(Token_str) > 1 }
  423.                                 Process_token_str := Token_str[1]
  424.                         End
  425.                 Else
  426.                         Process_token_str := Process_one_token_str(One_token)
  427.         End;  { Function Process_token_str }
  428.  
  429. Function BPos;
  430.  
  431.         Begin  { Function BPos }
  432.                 BPos := False;
  433.                 If Ignore_case Then
  434.                         Begin
  435.                                 Srch_str         := Up_cs(Srch_str);
  436.                                 Src_str   := Up_cs(Src_str)
  437.                         End;  { If Ignore_case }
  438.  
  439.                 {        Is this a Boolean expression?  If so process with this function, else
  440.                         process with Pascal POS function.
  441.                 }
  442.  
  443.                 If PosC('|', Srch_str) Or PosC('&', Srch_str) Then
  444.                         Begin
  445.                                 Srch_str := Parse_srch_str(Fixup_srch_str(Srch_str), Src_str);
  446.                                 If Srch_str <> '' Then
  447.                                         BPos := (Process_token_str(Srch_str) = 'T')
  448.                         End
  449.                 Else
  450.                         BPos := Pos(Srch_str, Src_str) <> 0
  451.         End;  { Function BPos }
  452.  
  453. End.  { Unit BoolPos }
  454.  
  455. Program Test;
  456. {$Define test}
  457.  
  458. {        Version 1.0.0.T
  459.  
  460.         Requires Borland Turbo Pascal version 6.0 or later to compile.
  461.  
  462.         Author:  Bruce J. Lackore.  Created Monday, June 13, 1994.
  463.         Copyright (c) 1994 Bruce J. Lackore.  ALL RIGHTS RESERVED.
  464. }
  465.  
  466. {$IFDEF Test}
  467.         {$A+,B-,D+,E+,F-,G-,I+,L+,N-,R+,S+,V-,X+}
  468. {$ELSE}
  469.         {$A+,B-,D-,E+,F-,G-,I-,L-,N-,R-,S-,V-,X+}
  470. {$ENDIF}
  471.  
  472. {$M 16384, 0, 655360}
  473.  
  474. {        This is a quick and really dirty test program for the Boolpos unit.  Just
  475.         tinker with the search phrase in line 3 of the code and enjoy!
  476. }
  477.  
  478. Uses Boolpos;
  479.  
  480. Var
  481.         BResult: Boolean;
  482.         Src_str: String;
  483.  
  484. Procedure Start_program;
  485.  
  486.         Begin  { Procedure Start_program }
  487.                 BResult := False;
  488.                 Src_str        := 'Now is the time for all good programmers to switch to OS/2';
  489.                 BResult := BPos('(Now&then)|(time&bad)', Src_str, False)
  490.         End;  { Procedure Start_program }
  491.  
  492. Begin  { Program:  Test }
  493.         Start_program;
  494. End.  { Program:  Test }